<template>
  <el-dialog
    :title="$t('common.modify') + $t('system.emailConfig')"
    :visible.sync="dialogVisible"
    :show="show"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    :before-close="handleClose"
    width="500px"
    @open="open"
  >
    <el-form
      ref="form"
      v-loading="infoLoading"
      :model="info"
      :rules="rules"
      label-width="160px"
    >
      <el-scrollbar
        ref="myScrollbar"
        wrap-class="wrapClass"
        class="form-scrollbar"
      >
        <div class="el-dialog-div">
          <el-form-item :label="$t('system.emailProvider')">
            <el-radio-group v-model="info.emailPlatform">
              <el-radio :label="0">mailjet</el-radio>
              <el-radio :label="1">rushmail</el-radio>
            </el-radio-group>
          </el-form-item>
          <template v-if="info.emailPlatform === 0">
            <el-form-item
              :label="$t('system.customMailjet')"
              prop="useIndependentMailjet"
            >
              <el-radio-group v-model="info.useIndependentMailjet">
                <el-radio :label="true">{{ $t('common.yes') }}</el-radio>
                <el-radio :label="false">{{ $t('common.no') }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <template v-if="info.useIndependentMailjet">
              <el-form-item label="mailjet apikey" prop="mailjetApiKey">
                <el-input v-model="info.mailjetApiKey" placeholder="apikey" />
              </el-form-item>
              <el-form-item label="mailjet apisecret" prop="mailjetApiSecret">
                <el-input
                  v-model="info.mailjetApiSecret"
                  placeholder="apisecret"
                />
              </el-form-item>
            </template>
          </template>

          <template v-if="info.emailPlatform === 1">
            <el-form-item
              :label="$t('system.customRushmail')"
              prop="useIndependentRushmail"
            >
              <el-radio-group v-model="info.useIndependentRushmail">
                <el-radio :label="true">{{ $t('common.yes') }}</el-radio>
                <el-radio :label="false">{{ $t('common.no') }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <template v-if="info.useIndependentRushmail">
              <el-form-item label="rushmail customId" prop="rushmailCustomId">
                <el-input
                  v-model="info.rushmailCustomId"
                  placeholder="customId"
                />
              </el-form-item>
              <el-form-item label="rushmail secretKey" prop="rushmailSecretKey">
                <el-input
                  v-model="info.rushmailSecretKey"
                  placeholder="secretKey"
                />
              </el-form-item>
            </template>
          </template>
        </div>
      </el-scrollbar>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">{{
            $t('common.confirm')
          }}</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<style scoped>
/deep/ .el-dialog__body {
  padding: 0;
}

.form-scrollbar {
  height: 500px;
}

.el-dialog-div {
  padding: 20px 20px 0 20px;
  min-width: 400px;
}

.avatar-uploader {
  width: 178px;
  height: 178px;
  border: 1px dashed #d9d9d9;
  border-radius: 10px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-color: #fbfdff;
}

.avatar-uploader:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<script>
import { postAction } from '@/api/common'
export default {
  name: 'editEmailSettings',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: {
      required: true,
      type: [Number, String],
      default: '',
    },
  },
  data() {
    return {
      dialogVisible: this.show,
      isChange: false,
      initForm: true,
      // 页面元素
      info: {
        emailPlatform: 0,
        useIndependentMailjet: 0,
        mailjetApiKey: 0,
        mailjetApiSecret: 0,
        useIndependentRushmail: 0,
        rushmailCustomId: 0,
        rushmailSecretKey: 0,
      },
      oemInfo: undefined,
      infoLoading: true,

      // 验证
      rules: {},
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
      this.initForm = true
    },
    info: {
      handler: function () {
        if (this.initForm) {
          this.isChange = false
          this.initForm = false
        } else {
          this.isChange = true
        }
      },
      deep: true,
    },
  },

  methods: {
    async open() {
      this.infoLoading = true
      // eslint-disable-next-line eqeqeq
      const { data } = await postAction({ id: this.id }, 'oem/GetInfo')
      this.info = data.emailSettings
      console.log(this.info)

      this.infoLoading = false
    },
    subInfo() {
      this.$refs.form.validate(async (valid) => {
        // form验证
        if (valid) {
          this.infoLoading = true

          // eslint-disable-next-line eqeqeq
          const info = this.info
          await postAction(info, 'oem/EditEmailSettings')

          this.infoLoading = false
          // 调用父级页面方法
          this.closeAction()
          this.$notify({
            title: this.$t('common.success'),
            message: this.$t('common.operationSuccess'),
            type: 'success',
            duration: 2000,
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      if (this.isChange) {
        this.$confirm(
          this.$t('common.unsavedChangesTip'),
          this.$t('common.tips'),
          {
            type: 'warning',
          }
        )
          .then((_) => {
            this.$emit('update:show', false)
          })
          .catch((e) => {
            console.log(e)
          })
      } else {
        this.$emit('update:show', false)
      }
    },
    closeAction() {
      this.$emit('closeAction')
    },
  },
}
</script>
